#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          kopano-dagent
# Required-Start:    $syslog $network $remote_fs
# Required-Stop:     $syslog $network $remote_fs
# Should-Start:      kopano-server
# Should-Stop:       kopano-server
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# X-UnitedLinux-Default-Enabled:   no
# Short-Description: Kopano Core Delivery Agent
### END INIT INFO

. /etc/rc.status
rc_reset

DAGENTCONFIG=/etc/kopano/dagent.cfg
DAGENTPROGRAM=/usr/sbin/kopano-dagent

# Sanity checks.
#[ -f $DAGENTCONFIG ] || exit 0
[ -x $DAGENTPROGRAM ] || exit 0

# the -d option is required to run LMTP mode deamonized
DAGENTCONFIG_OPT="-d"
[ ! -z $DAGENTCONFIG -a -f $DAGENTCONFIG ] && DAGENTCONFIG_OPT="$DAGENTCONFIG_OPT -c $DAGENTCONFIG"

[ -f /etc/sysconfig/kopano ] && . /etc/sysconfig/kopano
if [ -z "$KOPANO_LOCALE" ]; then
	KOPANO_LOCALE="C"
fi

dagent=`basename $DAGENTPROGRAM`

case "$1" in
    start)
		echo -n "Starting LMTP Dagent "
		install -dm0775 -o kopano -g kopano /var/run/kopano
		export LC_ALL="$KOPANO_LOCALE"
		export LANG="$KOPANO_LOCALE"
		$DAGENTPROGRAM $DAGENTCONFIG_OPT
		rc_status -v
		unset LC_ALL LANG
		;;

    stop)
		echo -n "Stopping LMTP Dagent "
		killproc $dagent
		rc_status -v
		;;

    restart)
		$0 stop
		$0 start
		;;

	reload)
		echo -n "Restarting LMTP Dagent: "
		killproc $DAGENTPROGRAM -SIGHUP
		rc_status -v
		;;

    status)
		echo -n "Checking for LMTP Dagent "
		checkproc $dagent
		rc_status -v
		;;

    *)
		;;
esac

rc_exit
